package com.exteam.common.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

/**
 * Created by wjk on 2015/5/5.
 * <p/>
 * 打开手机应用安装详情界面
 * 判断手机系统类型（华为和小米）
 */
public class PhoneModelUtils {


    /**
     * 打开手机应用安装详情界面,以便用户开启相关系统权限
     *
     * @param context
     * @param packageName
     */
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static void showInstalledAppDetails(Context context, String packageName) {
        if (packageName == null)
            return;
        final int apiLevel = Build.VERSION.SDK_INT;
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (PhoneModelUtils.isEMUI() && checkPackage("com.huawei.systemmanager", context)) {
            intent.setAction("Intent.ACTION_VIEW");
            intent.setClassName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity");
        } else if (apiLevel >= 9) {
            // 如果是MIUIV6，还得判断下这个app是否存在。
            if (PhoneModelUtils.isMiuiV6() && checkPackage("com.miui.securitycenter", context)) {
                intent.setAction("miui.intent.action.APP_PERM_EDITOR");
                intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                intent.putExtra("extra_pkgname", packageName);
            } else {
                intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + packageName));
            }
        } else {
            final String appPkgName = (apiLevel == 8 ? "pkg" : "com.android.settings.ApplicationPkgName");
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra(appPkgName, packageName);
        }
        context.startActivity(intent);
    }


    //hua wei EMUI2.3 以上
    public static boolean isEMUI() {

        String emui = System.getProperties().getProperty("ro.build.version.emui", "unkonw");
        String emui2 = CommUtils.getSystemProperty("ro.build.version.emui");
        if (null == emui && null == emui2)
            return false;
        String emotion[] = emui.split("_");
        if (emotion.length > 1) {
            if (emotion[0].equalsIgnoreCase("EmotionUI")) {
                String nemui[] = emotion[1].split("\\.");
                String aemui[] = "2.3".split("\\.");
                int length = nemui.length > aemui.length ? aemui.length : nemui.length;
                for (int i = 0; i < length; i++) {
                    int n = Integer.valueOf(nemui[i]);
                    int a = Integer.valueOf(aemui[i]);
                    if (n == a) {
                        continue;
                    } else {
                        return n > a;
                    }
                }
                return true;
            }
        } else {
            emotion = emui2.split("_");
            if (emotion[0].equalsIgnoreCase("EmotionUI")) {
                String nemui[] = emotion[1].split("\\.");
                String aemui[] = "2.3".split("\\.");
                int length = nemui.length > aemui.length ? aemui.length : nemui.length;
                for (int i = 0; i < length; i++) {
                    int n = Integer.valueOf(nemui[i]);
                    int a = Integer.valueOf(aemui[i]);
                    if (n == a) {
                        continue;
                    } else {
                        return n > a;
                    }
                }
                return true;
            }
        }
        return false;
    }

    public static boolean isMiui() {
        String s = Build.DISPLAY;
        if (s != null) {
            if (s.toUpperCase().contains("MIUI")) {
                return true;
            }
        }
        s = Build.MODEL; // 小米
        if (s != null) {
            if (s.contains("MI-ONE")) {
                return true;
            }
        }
        s = Build.DEVICE;
        if (s != null) {
            if (s.contains("mione")) {
                return true;
            }
        }
        s = Build.MANUFACTURER;
        if (s != null) {
            if (s.equalsIgnoreCase("Xiaomi")) {
                return true;
            }
        }
        s = Build.PRODUCT;
        if (s != null) {
            if (s.contains("mione")) {
                return true;
            }
        }
        return false;
    }

    public static boolean isMiuiV5() {
        String ver = System.getProperties().getProperty("ro.miui.ui.version.name", "UNKNOWN");
        String ver2 = CommUtils.getSystemProperty("ro.miui.ui.version.name");
        if (ver.equalsIgnoreCase("V5") || ver.contains("V5")) {
            return true;
        } else if (ver2.equalsIgnoreCase("V5") || ver2.contains("V5")) {
            return true;
        }
        return false;
    }

    public static boolean isMiuiV6() {
        String ver = System.getProperties().getProperty("ro.miui.ui.version.name", "UNKNOWN");
        String ver2 = CommUtils.getSystemProperty("ro.miui.ui.version.name");
        if (ver.equalsIgnoreCase("V6") || ver.contains("V6")) {
            return true;
        } else if (ver2.equalsIgnoreCase("V6") || ver2.contains("V6")) {
            return true;
        }
        return false;
    }

    /**
     * 检测该包名所对应的应用是否存在
     *
     * @param packageName
     * @return
     */

    public static boolean checkPackage(String packageName, Context context) {
        if (packageName == null || "".equals(packageName))
            return false;
        try {
            context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
